﻿using System;
using System.Linq;
using System.Xml.Linq;
using TVersityDotNet.MediaLib;
using TVersityDotNet.HelperTypes;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Globalization;
using System.Runtime.Serialization;

namespace TVersityDotNet.Responses
{
    [DataContractAttribute]
    public class MediaLibResponse : TVersityResponse
    {
        [DataMember]
        public Collection<MediaLibResponseItem> ResponseItems { get; private set; }

        private Lazy<Collection<MediaLibVideo>> _videos = new Lazy<Collection<MediaLibVideo>>();
        [DataMember]
        public Collection<MediaLibVideo> Videos
        {
            get
            {
                foreach (MediaLibResponseItem item in this.ResponseItems)
                {
                    if (item.SearchClass == MediaLibClassType.VideoItem)
                        this._videos.Value.Add(new MediaLibVideo(item.RawXml));
                }                

                return this._videos.Value;
            }
        }
        
        private Lazy<Collection<MediaLibMusicItem>> _musicItems = new Lazy<Collection<MediaLibMusicItem>>();
        [DataMember]
        public Collection<MediaLibMusicItem> MusicItems
        {
            get
            {
                foreach (MediaLibResponseItem item in this.ResponseItems)
                {
                    if (item.SearchClass == MediaLibClassType.MusicItem)
                    {
                        this._musicItems.Value.Add(new MediaLibMusicItem(item.RawXml));
                    }
                }

                return this._musicItems.Value;
            }
        }

        private Lazy<Collection<MediaLibMusicArtist>> _musicArtists = new Lazy<Collection<MediaLibMusicArtist>>();
        [DataMember]
        public Collection<MediaLibMusicArtist> MusicArtists
        {
            get
            {
                foreach (MediaLibResponseItem item in this.ResponseItems)
                {
                    if (item.SearchClass == MediaLibClassType.MusicArtist)
                    {
                        this._musicArtists.Value.Add(new MediaLibMusicArtist(item.RawXml));
                    }
                }                

                return this._musicArtists.Value;
            }
        }

        private Lazy<Collection<MediaLibPhoto>> _photos = new Lazy<Collection<MediaLibPhoto>>();
        [DataMember]
        public Collection<MediaLibPhoto> Photos
        {
            get
            {
                foreach (MediaLibResponseItem item in this.ResponseItems)
                {
                    if (item.SearchClass == MediaLibClassType.Photo)
                    {
                        this._photos.Value.Add(new MediaLibPhoto(item.RawXml));
                    }
                }                

                return this._photos.Value;
            }
        }

        private Lazy<Collection<MediaLibContainer>> _containers = new Lazy<Collection<MediaLibContainer>>();
        [DataMember]
        public Collection<MediaLibContainer> Containers
        {
            get
            {
                foreach (MediaLibResponseItem item in this.ResponseItems)
                {
                    if (item.SearchClass == MediaLibClassType.Container)
                    {
                        this._containers.Value.Add(new MediaLibContainer(item.RawXml));
                    }
                }
                
                return this._containers.Value;
            }
        }

        private Lazy<Collection<MediaLibFolder>> _folders = new Lazy<Collection<MediaLibFolder>>();
        [DataMember]
        public Collection<MediaLibFolder> Folders
        {
            get
            {
                foreach (MediaLibResponseItem item in this.ResponseItems)
                {
                    if (item.SearchClass == MediaLibClassType.Container)
                    {
                        this._folders.Value.Add(new MediaLibFolder(item.RawXml));
                    }
                }

                return this._folders.Value;
            }
        }
        
        public MediaLibResponse(XDocument xmlDocument)
        {
            if (xmlDocument != null)
            {
                this.ResponseItems = new Collection<MediaLibResponseItem>();
                this.ResponseXml = xmlDocument;
                this.ParseMessage(this.ResponseXml);
            }
            else
            {
                this.ResponseSuccess = false;
                this.ResponseMessage = "Incomming xmlDocument was null";
            }
        }

        public override void ParseMessage(XDocument responseXml)
        {
            if (responseXml != null)
            {
                foreach (XElement element in responseXml.Descendants(XName.Get("item", MediaLibResponseItem.Namespace_Base)))
                {                   
                    this.ResponseItems.Add(new MediaLibResponseItem(element));
                }
                foreach (XElement element in responseXml.Descendants(XName.Get("container", MediaLibResponseItem.Namespace_Base)))
                {
                    this.ResponseItems.Add(new MediaLibResponseItem(element));
                }
            }
            else
            {
                this.ResponseSuccess = false;
                this.ResponseMessage = "Provided responseXml was null";
            }
        }
    }    
}
